<%@ page language="java" contentType="text/html; charset=utf-8"
         pageEncoding="utf-8" isELIgnored="false"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">


    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/layui/css/layui.css"/>
    <link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/admin/css/admin.css" />

    <script type="text/javascript" src="${pageContext.request.contextPath}/js/jquery-3.3.1.min.js"></script>
    <script src="${pageContext.request.contextPath}/static/admin/layui/layui.js" type="text/javascript" charset="utf-8"></script>
    <script src="${pageContext.request.contextPath}/static/admin/js/common.js" type="text/javascript" charset="utf-8"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
    <style>
        td{
            text-align: center;
        }
        th{
            text-align: center;
        }
    </style>
</head>
<body>
<%
    int i=1;
    int num=0;
%>
<div class="layui-form">
    <table class="layui-table">
        <colgroup>
            <col width="150">
            <col width="150">
            <col width="200">
            <col>
        </colgroup>
        <thead>
        <tr>
            <th>商品</th>
            <th>图片仅供参考</th>
            <th>价格(元)</th>
            <th>供应商</th>
            <th>商品类别</th>
            <th>库存</th>
            <th>总金额</th>
            <th>购买</th>
            <th>操作&nbsp;&nbsp;<button type="button" onclick="add()"
                                      class="layui-btn layui-btn-primary layui-btn-sm">添加</button></th>
        </tr>
        </thead>
        <tbody>
        <c:forEach items="${commodityList}" var="comm">
            <tr>
                <td>
                        ${comm.cname}
                </td>
                <td>
                        <img src="${pageContext.request.contextPath}/${comm.cface}" width="50px" height="50px">
                </td>
                <td>
                        ${comm.cprice}
                </td>
                <td>
                        ${comm.cprovider}
                </td>
                <td>
                        ${comm.ctype}
                </td>
                <td>
                        ${comm.cnum}
                </td>
                <td>
                        <%=num%>
                </td>
                <td>
                    <a onclick="addorsub('sub',this)" style="cursor: pointer">-</a>&nbsp;&nbsp;
                    <a onclick="jump('${user.uid}','${comm.cid}','${comm.cprovider}',this)"
                                class="layui-btn" target="_blank">购买<span id="spid"><%=i-1%></span>件</a>&nbsp;&nbsp;
                    <a onclick="addorsub('add',this)" style="cursor: pointer">+</a>&nbsp;&nbsp;
                </td>
                <c:choose>
                    <c:when test="${sessionScope.user.utype==1}">
                        <td>
                            <button type="button" class="layui-btn" href="UserServlet.action?flag=deleteById&cid=${comm.cid}">删除</button>&nbsp;&nbsp;
                            <button type="button" onclick="up('${comm.cid}')" class="layui-btn">修改</button>
                            <%--'${comm.cname}','${comm.cprice}','${comm.cprovider}','${comm.cid}'&ndash;%&gt;--%>
                        </td>
                    </c:when>
                    <c:otherwise>
                        <td>管理员可以操作</td>
                    </c:otherwise>
                </c:choose>
            </tr>
        </c:forEach>
        </tbody>
    </table>
</div>

</body>
</html>



<script>
    function up(cid) {
        var index = layer.open({
            type: 2,
            title: '商品修改',
            area: ['500px', '300px'],
            fixed: false, //不固定
            maxmin: true,
            offset: '10px',
            end:function (){

            },
            content: "${pageContext.request.contextPath}/findByIdServlet.action?id="+cid
        });
        return false;
    }
    
    function add() {
        var index = layer.open({
            type: 2,
            title: '商品添加',
            area: ['800px', '600px'],
            fixed: false, //不固定
            maxmin: true,
            offset: '10px',
            end:function (){

            },
            content: "${pageContext.request.contextPath}/commadd.jsp"
        });
        return false;
    }


    function jump(uid,cid,cprovider,aobj){
        var num = $(aobj).parent().parent().children().eq(6).html().trim();
        if(num==0){
            window.location.href="commodityFindAll.jsp"
        }
        window.location.href="CommServlet.action?flag=buy&uid="+uid+"&cid="+cid+"&cprovider="+cprovider+"&num="+num
    }

    function addorsub(ad,aobj) {
       var i = $(aobj).parent().children().children().eq(0).html()
        var num = $(aobj).parent().parent().children().eq(6).html()
        var p = $(aobj).parent().parent().children().eq(2).html()

        if(ad=="add"){
            i++;
            num=parseInt(num)+parseInt(p);
        }else {
            if(parseInt(num)<parseInt(p)){
                num=parseInt(num)
            }else{
                num=parseInt(num)-parseInt(p)
            }
            i==0?i:i--;
        }
       $(aobj).parent().parent().children().eq(6).html(num)
        $(aobj).parent().children().children().eq(0).html(i)
    }

    /*$(function () {
        var feedback=request.getAttribute("msg");
        if(feedback=="ye"){
            layer.msg('余额不足!', {icon: 5});
        }
    })*/


</script>
